import sys
from bs4 import BeautifulSoup
import urllib.request as urllib2
import urllib
import re
import os

"""
url = 'http://www.doorstore.co.uk/products/internal-interior-doors/oak-doors/641/'

response = urllib2.urlopen(url)
html = response.read()

soup = BeautifulSoup(html)

img_classes = soup.findAll('ul', {'class': 'pro-list'})"""

test_url = 'http://www.doorstore.co.uk/product/oak-doors/trade-spec-shaker-1-panel-oak-door-40mm/4669-641/'

test_url = 'http://www.doorstore.co.uk/product/walnut-doors/shaker-1-panel-walnut-door-40mm/3154-642/'

response2 = urllib2.urlopen(test_url)
html2 = response2.read()

soup2 = BeautifulSoup(html2)

img_classes2 = soup2.findAll('ul', {'id': 'thumblist'})

for im in img_classes2:
	elems = im.findAll('a')
	for eli in elems:
		src = eli.get('href')
		rel = eli.get('rel')
		imgsm = eli.find('img').get('src')

		filenamesm = imgsm.replace('ProdImages','')
		filenamesm = filenamesm.replace('prodimages','')
		filenamesm = filenamesm.replace('/','')


		relegx = '(rel=")(.|\n)+((">)|(" >)){1}'
		smalimageex = '(smallImage: )(.|\n)+(?!>)'		

		relegxstr  = re.search('(rel=")(.|\n)+((">)|(" >)){1}', str(eli)).group(0)

		smallImagestr = re.search('(smallImage: )(.|\n)+(?!>)', relegxstr).group(0)
		smallImagestr = smallImagestr.replace('smallImage: ','')
		smallImagestr = smallImagestr.replace(' ">','')
		
		smallImagestr = smallImagestr.replace('\'','')

		imgmid = smallImagestr
		smallImagestr = smallImagestr.replace('ProdImages','')
		smallImagestr = smallImagestr.replace('prodimages','')
		smallImagestr = smallImagestr.replace('/','')
		filenamemid = smallImagestr

		print('---------------------------------------------------------------------')
		print('imgsm = ' + imgsm)
		print('filenamesm=' + filenamesm)
		print('imgmid=' + imgmid)
		print('filenamemid=' + filenamemid)
		#directory = 'testdir'

		newpath = r'testdir2' 
		if not os.path.exists(newpath): os.makedirs(newpath)

		url_path = 'http://www.doorstore.co.uk'
		full_urlsm = url_path + imgsm
		full_urlsm = full_urlsm.replace(' ','%20')
		print('full_urlsm=' + full_urlsm)
		print('filenamesm=' + filenamesm)
		#if i == 0:
		image_on_web = urllib2.urlopen(full_urlsm)
		buf = image_on_web.read()

		downloaded_image = open(newpath + '\\'+filenamesm, "wb")
		downloaded_image.write(buf)
		downloaded_image.close()
		image_on_web.close()

		#savePhotoToBd(filenamesm)

		

		#---------------------------------------------------------------------------
		full_urlmid = url_path + imgmid
		full_urlmid = full_urlmid.replace(' ','%20')
		print('full_urlsm=' + full_urlmid)
		print('filenamesm=' + filenamemid)
		#if i == 0:
		image_on_web = urllib2.urlopen(full_urlmid)
		buf = image_on_web.read()

		downloaded_image = open(newpath + '\\'+filenamemid, "wb")
		downloaded_image.write(buf)
		downloaded_image.close()
		image_on_web.close()

		#---------------------------------------------------------------------------

		#print(src)
		#print(rel)
		#print(img)
		print('!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!')

"""test_url_backspace = 'http://93.72.16.48/door-market/productimg/door 1 lar.jpg'

print(test_url_backspace)

test_url_without_backspace = test_url_backspace.replace(' ','%20')

print(test_url_without_backspace)

test_url =  'http://www.doorstore.co.uk/ProdImages/Contemporary%204%20Lite.jpg'
image_on_web = urllib2.urlopen(test_url)

print(image_on_web)
buf = image_on_web.read()

downloaded_image = open('test34.jpg', "wb")
downloaded_image.write(buf)
downloaded_image.close()
image_on_web.close()"""

print('*************************************************************')

"""
i=0
for im in img_classes:
	src = im.find('a').get('href')
	print('---------------------------------------------------------------------')
	print(src)

	file_name = src.replace('/ProdImages/','')
	file_name = file_name.replace('/prodimages/','')
	
	
	
	url_path = 'http://www.doorstore.co.uk'
	full_url = url_path + src
	full_url = full_url.replace(' ','%20')
	print(full_url)
	print(file_name)
	#if i == 0:
	image_on_web = urllib2.urlopen(full_url)
	buf = image_on_web.read()

	downloaded_image = open(file_name, "wb")
	downloaded_image.write(buf)
	downloaded_image.close()
	image_on_web.close()

	print('---------------------------------------------------------------------')"""

print('12')